package lk.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public RestfulRecord exceptionHandler(HttpServletRequest req, Exception e) {
        if (e instanceof InvalidTokenException) {
            return new RestfulRecord(ResponseCodeContants.TOKEN_IS_EXPIRED_CODE, "登陆失效");
        }
        e.printStackTrace();
        RestfulRecord restfulRecord = new RestfulRecord();
        restfulRecord.setException(e);
        restfulRecord.setMsg("异常");
        return restfulRecord;
    }

}
